﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Polaris.Domain.Interfaces.Admin;
using Polaris.Domain.Model.Dto.Sys_Dictionary.Request;
using Polaris.Domain.Model.Dto.Sys_Dictionary_Item.Request;
using Polaris.WebApi.Swagger;

namespace Polaris.WebApi.Controllers.Admin
{
    /// <summary>
    /// 字典接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiGroup(ApiGroupNames.PolarisAdmin)]
    [ApiController]
    public class Sys_DictionaryController : BaseController
    {
        private readonly ISys_Dictionary _sys_Dictionary;
        private readonly ISys_Dictionary_Item _sys_Dictionary_Item;

        public Sys_DictionaryController(ISys_Dictionary sys_Dictionary, ISys_Dictionary_Item sys_Dictionary_Item)
        {
            _sys_Dictionary = sys_Dictionary;
            _sys_Dictionary_Item = sys_Dictionary_Item;
        }

        #region 字典接口

        /// <summary>
        /// 创建字典
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateDictionary(CreateDictionaryRequest create)
        {
            await _sys_Dictionary.CreateDictionary(create, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateDictionary(UpdateDictionaryRequest update)
        {
            await _sys_Dictionary.UpdateDictionary(update, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteDictionary(string id)
        {
            await _sys_Dictionary.DeleteDictionary(id, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryList([FromQuery] SearchDictionaryRequest search)
        {
            var result = await _sys_Dictionary.GetDictionaryList(search);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id获取字典详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryById([FromQuery] string id)
        {
            var result = await _sys_Dictionary.GetDictionaryById(id);
            return SUCCESS(result);
        }

        #endregion

        #region 字典项接口

        /// <summary>
        /// 创建字典项
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateDictionaryItem(CreateDictionaryItemRequest create)
        {
            await _sys_Dictionary_Item.CreateDictionaryItem(create, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 修改字典项
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateDictionaryItem(UpdateDictionaryItemRequest update)
        {
            await _sys_Dictionary_Item.UpdateDictionaryItem(update, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 删除字典项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteDictionaryItem(string id)
        {
            await _sys_Dictionary_Item.DeleteDictionaryItem(id, null);
            return SUCCESS("OK");
        }

        /// <summary>
        /// 根据字典id获取字典项列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryItemListByDid([FromQuery] SearchDictionaryItemRequest search)
        {
            var result = await _sys_Dictionary_Item.GetDictionaryItemListByDid(search);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id获取字典项详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryItemById([FromQuery] string id)
        {
            var result = await _sys_Dictionary_Item.GetDictionaryItemById(id);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据字典值获取字典项
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetDictionaryItemByCode([FromQuery] string code)
        {
            var result = await _sys_Dictionary_Item.GetDictionaryItemByCode(code);
            return SUCCESS(result);
        }
        #endregion

    }
}
